package com.ceuer.demo.sync;

import java.util.concurrent.locks.ReentrantLock;

public class SyncTest {
	public static void main(String[] args) {
		SyncObj syncObj=new SyncObj();
		//new Thread(()->{syncObj.test1();}).start();
		
		new Thread(()->{syncObj.test22();}).start();
		
		new Thread(()->{syncObj.test22();}).start();
		
		//new Thread(()->{syncObj.test3();}).start();
	}
}

class SyncObj{
	private final ReentrantLock lock=new ReentrantLock();
	DemoS demoS=new DemoS();
	synchronized void test1(){
		System.out.println("start test1 ...");
		try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(Thread.currentThread().getName()+"\ttest1");
	}
	
	void test2(){
		synchronized(demoS){
			System.out.println(Thread.currentThread().getName()+" \t start");
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName()+"\t end");
		}
	}
	
	void test22(){
		try {
			System.out.println(Thread.currentThread().getName()+" \t test22");
			lock.lock();
			System.out.println(Thread.currentThread().getName()+" \t start");
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName()+"\t end");
		}finally {
			lock.unlock();
		}
	}
	
	void test3(){
		System.out.println("start test3 ...");
		System.out.println(Thread.currentThread().getName()+"\ttest3");
	}
	
}

class DemoS{

}